package com.ztf.room.dao;

import com.ztf.room.bean.RoomApplyResults;
import com.ztf.room.bean.RoomApplys;
import com.ztf.room.bean.Student;
import com.ztf.room.bean.StudyRooms;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface StudyRoomDao {

    @Select("select * from studyrooms")
    @Results({
        @Result(column="studyroomId" ,property="studyroomId"),
        @Result(column="studyroomType" ,property="studyroomType"),
        @Result(column="studyroomNumber" ,property="studyroomNumber"),
        @Result(column="studyroomAmount" ,property="studyroomAmount"),
        @Result(column="studyroomNote" ,property="studyroomNote"),
        @Result(column="studyroomState" ,property="studyroomState"),
        @Result(property="students" ,column="studyroomId",many = @Many(select = "com.ztf.room.dao.StudyRoomDao.selectOnlineRoomStu")),
    })
    List<StudyRooms> selectRoomList();

    @Select("select s.* from student s join room_applys r on s.studentId=r.studentId and r.status='1' where r.closeTime >now() and now()>r.startTime and r.studyroomId=#{studyroomId}")
    List<Student> selectOnlineRoomStu(Integer studyroomId);

    @Insert("insert into room_applys values(null,#{student.studentId},#{student.studentName},#{student.studentcontact},#{student.studentAcademy},#{studyRooms.studyroomId},#{studyRooms.studyroomNumber},#{startTime},#{closeTime},#{applyReason},#{applyDate},'3')")
    int addApplyRoom(RoomApplys roomApplys);

    @Insert("insert into room_applyresults values(null,#{roomApplys.roomApplyId},#{approveOrnot},#{managerId},#{managerName},#{managerContact})")
    void addApplyRoomResult(RoomApplyResults roomApplyResults);

    @Select("select * from room_applys where studentId=#{studentId} order by applyDate desc")
    @Results(id = "room_apply",value = {
            @Result(column="roomApplyId" ,property="roomApplyId"),
            @Result(column="studentId" ,property="student.studentId"),
            @Result(column="studentName" ,property="student.studentName"),
            @Result(column="studentContact" ,property="student.studentcontact"),
            @Result(column="studentAcademy" ,property="student.studentAcademy"),
            @Result(column="studyroomId" ,property="studyRooms.studyroomId"),
            @Result(column="studyroomNumber" ,property="studyRooms.studyroomNumber"),
            @Result(column="startTime" ,property="startTime"),
            @Result(column="closeTime" ,property="closeTime"),
            @Result(column="applyReason" ,property="applyReason"),
            @Result(column="applyDate" ,property="applyDate"),
            @Result(column="status" ,property="status"),
    })
    List<RoomApplys> getApplyResultsByStu(int studentId);

    @Select("select * from room_applys where status='3' order by applyDate desc")
    @ResultMap("room_apply")
    List<RoomApplys> getApplyResults();

    @Update("update room_applys set  status=#{approveOrnot} where roomApplyId=#{roomApplys.roomApplyId}")
    void updateRoomApply(RoomApplyResults results);

    @Select("select count(*) from student s join room_applys r on s.studentId=r.studentId and r.status='1'")
    int checkRoomApply(RoomApplys roomApplys);

}
